{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Zen of Python, by Tim Peters\n",
      "\n",
      "Beautiful is better than ugly.\n",
      "Explicit is better than implicit.\n",
      "Simple is better than complex.\n",
      "Complex is better than complicated.\n",
      "Flat is better than nested.\n",
      "Sparse is better than dense.\n",
      "Readability counts.\n",
      "Special cases aren't special enough to break the rules.\n",
      "Although practicality beats purity.\n",
      "Errors should never pass silently.\n",
      "Unless explicitly silenced.\n",
      "In the face of ambiguity, refuse the temptation to guess.\n",
      "There should be one-- and preferably only one --obvious way to do it.\n",
      "Although that way may not be obvious at first unless you're Dutch.\n",
      "Now is better than never.\n",
      "Although never is often better than *right* now.\n",
      "If the implementation is hard to explain, it's a bad idea.\n",
      "If the implementation is easy to explain, it may be a good idea.\n",
      "Namespaces are one honking great idea -- let's do more of those!\n"
     ]
    }
   ],
   "source": [
    "import this"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### I. Native DataTypes\n",
    "#### Number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "## integer\n",
    "a = 1\n",
    "b = 0x10            # 16\n",
    "print(type(a))      # <class 'int'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "## float\n",
    "c = 1.2\n",
    "d = .5              # 0.5\n",
    "g = .314e1          # 3.14\n",
    "print(type(g))      # <class 'float'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'complex'>\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "## complex\n",
    "e = 1+2j\n",
    "f = complex(1, 2)\n",
    "print(type(e))      # <class 'complex'>\n",
    "print(f == e)       # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "0\n",
      "9\n",
      "1.25\n",
      "1024\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "## Operators: + - * / ** // %\n",
    "print(1 + 1)\n",
    "\n",
    "print(2 - 2)\n",
    "\n",
    "print(3 * 3)\n",
    "\n",
    "print(5 / 4)\n",
    "\n",
    "print(2 ** 10)\n",
    "\n",
    "print(5 // 4)\n",
    "\n",
    "print(5 % 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float(x) -> floating point number\n",
      "\n",
      "Convert a string or number to a floating point number, if possible.\n",
      "3.0\n",
      "3.0\n",
      "3.14\n"
     ]
    }
   ],
   "source": [
    "## Casting\n",
    "### Integer -> Float\n",
    "print(float.__doc__)\n",
    "\n",
    "print(float(3))\n",
    "print(3 / 1)\n",
    "print(float(\"3.14\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int(x=0) -> integer\n",
      "int(x, base=10) -> integer\n",
      "\n",
      "Convert a number or string to an integer, or return 0 if no arguments\n",
      "are given.  If x is a number, return x.__int__().  For floating point\n",
      "numbers, this truncates towards zero.\n",
      "\n",
      "If x is not a number or if base is given, then x must be a string,\n",
      "bytes, or bytearray instance representing an integer literal in the\n",
      "given base.  The literal can be preceded by '+' or '-' and be surrounded\n",
      "by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\n",
      "Base 0 means to interpret the base from the string as an integer literal.\n",
      ">>> int('0b100', base=0)\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "### Float -> Integer\n",
    "print(int.__doc__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3\n",
      "10\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "print(int(3.14))               # 3\n",
    "print(int(\"3\", base = 10))     # 3\n",
    "print(int(\"1010\", base = 2))   # 10\n",
    "print(int(\"0b1010\", base = 0)) # 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### String"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "🐶\n",
      ", Dogge's home, \n",
      "Hello,\n",
      "Dogge!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "s1 = '🐶\\n'\n",
    "s2 = \"Dogge's home\"\n",
    "s3 = \"\"\"\n",
    "Hello,\n",
    "Dogge!\n",
    "\"\"\"\n",
    "print(type(s1))     # <class 'str'>\n",
    "print(\"%s, %s, %s\" % (s1, s2, s3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "## Length\n",
    "print(len(s1))      # 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学:习\n"
     ]
    }
   ],
   "source": [
    "## Slicing\n",
    "s = '学而时习之'\n",
    "print('{0}:{1}'.format(s[0], s[-2]))    # 学:习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc.xyz\n"
     ]
    }
   ],
   "source": [
    "## Operator: +\n",
    "print(\"abc\" + \".\" + \"xyz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "str(object='') -> str\n",
      "str(bytes_or_buffer[, encoding[, errors]]) -> str\n",
      "\n",
      "Create a new string object from the given object. If encoding or\n",
      "errors is specified, then the object must expose a data buffer\n",
      "that will be decoded using the given encoding and error handler.\n",
      "Otherwise, returns the result of object.__str__() (if defined)\n",
      "or repr(object).\n",
      "encoding defaults to sys.getdefaultencoding().\n",
      "errors defaults to 'strict'.\n",
      "3.14\n",
      "3\n",
      "[1, 2, 3]\n",
      "(1, 2, 3)\n",
      "{1, 2, 3}\n",
      "{'python': '*.py', 'javascript': '*.js'}\n"
     ]
    }
   ],
   "source": [
    "## Casting\n",
    "print(str.__doc__)\n",
    "\n",
    "print(str(3.14))\n",
    "print(str(3))\n",
    "print(str([1,2,3]))\n",
    "print(str((1,2,3)))\n",
    "print(str({1,2,3}))\n",
    "print(str({'python': '*.py', 'javascript': '*.js'}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Byte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bytes'>\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# Byte\n",
    "## 0-255/x00-xff\n",
    "byt = b'abc'\n",
    "print(type(byt))    # <class 'bytes'>\n",
    "print(byt[0] == 'a')# False\n",
    "print(byt[0] == 97) # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "## Length\n",
    "print(len(byt))     # 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Boolean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "True\n",
    "False\n",
    "print(type(True))   # <class 'bool'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "<class 'NoneType'>\n"
     ]
    }
   ],
   "source": [
    "print(None is None) # True\n",
    "print(type(None))   # <class 'NoneType'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "l = ['python', 3, 'in', 'one']\n",
    "print(type(l))      # <class 'list'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "## Length\n",
    "print(len(l))       # 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python\n",
      "one\n",
      "[3, 'in']\n"
     ]
    }
   ],
   "source": [
    "## Slicing\n",
    "print(l[0])         # 'python'\n",
    "print(l[-1])        # 'one'\n",
    "print(l[1:-1])      # [3, 'in']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 3, 'in', 'one', 'pic']\n",
      "['python', 3, '.4.1', 'in', 'one', 'pic']\n",
      "['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']\n"
     ]
    }
   ],
   "source": [
    "## Alter\n",
    "l.append('pic')     # None\n",
    "print(l)\n",
    "# l == ['python', 3, 'in', 'one', 'pic']\n",
    "\n",
    "l.insert(2, '.4.1') # None\n",
    "print(l)\n",
    "# l == ['python', 3, '.4.1', 'in', 'one', 'pic']\n",
    "\n",
    "l.extend(['!', '!'])\n",
    "print(l)\n",
    "# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!\n",
      "['python', 3, '.4.1', 'in', 'one', 'pic', '!']\n",
      ".4.1\n",
      "['python', 3, 'in', 'one', 'pic', '!']\n",
      "['python', 3, 'one', 'pic', '!']\n",
      "['python', 3, 'pic', '!']\n"
     ]
    }
   ],
   "source": [
    "print(l.pop())             # '!'\n",
    "print(l)\n",
    "# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!']\n",
    "\n",
    "print(l.pop(2))           # '.4.1'\n",
    "print(l)\n",
    "# l == ['python', 3, 'in', 'one', 'pic', '!']\n",
    "\n",
    "l.remove(\"in\")\n",
    "print(l)\n",
    "# l == ['python', 3, 'one', 'pic', '!']\n",
    "\n",
    "del l[2]\n",
    "print(l)\n",
    "# l == ['python', 3, 'pic', '!']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(l.index('pic'))       # 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tp = (1, 2, 3, [4, 5])\n",
    "print(type(tp)) # <class 'tuple'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "3\n",
      "(1, 2, 3, [4, 6])\n"
     ]
    }
   ],
   "source": [
    "## Length\n",
    "print(len(tp))  # 4\n",
    "\n",
    "print(tp[2])    # 3\n",
    "tp[3][1] = 6\n",
    "print(tp)       # (1, 2, 3, [4, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n"
     ]
    }
   ],
   "source": [
    "## Single element\n",
    "tp = (1, )      # Not tp = (1)\n",
    "print(tp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a 2 3\n"
     ]
    }
   ],
   "source": [
    "## Assign multiple values at once\n",
    "v = (3, 2, 'a')\n",
    "(c, b, a) = v\n",
    "print(a, b, c)  # a 2 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "st = {'s', 'e', 'T'}\n",
    "print(type(st)) # <class 'set'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "## Length\n",
    "print(len(st))  # 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "## Empty\n",
    "st = set()\n",
    "print(len(st))  # 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "st = {}\n",
    "print(type(st)) # <class 'dict'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'T', '!', 's', 't', 'e'}\n",
      "{'s', 'e'}\n",
      "set()\n"
     ]
    }
   ],
   "source": [
    "## Alter\n",
    "st = set(['s', 'e', 'T'])\n",
    "st.add('t')     # st == {'s', 'e', 't', 'T'}\n",
    "st.add('t')     # st == {'s', 'e', 't', 'T'}\n",
    "st.update(['!', '!'])\n",
    "print(st)\n",
    "# st == {'s', 'e', 't', 'T', '!'}\n",
    "\n",
    "st.discard('t') # st == {'T', '!', 's', 'e'} # No Error\n",
    "st.remove('T')  # st == {'s', 'e', '!'}      # KeyError\n",
    "st.pop()        # 's'\n",
    "print(st)\n",
    "# st == {'e'}\n",
    "\n",
    "st.clear()      # st == set()\n",
    "print(st)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'k2': 'v2', 'k1': 'v1'}\n"
     ]
    }
   ],
   "source": [
    "dic = {}\n",
    "print(type(dic))    # <class 'dict'>\n",
    "\n",
    "dic = {'k1': 'v1', 'k2': 'v2'}\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "## Length\n",
    "print(len(dic))     # 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v2\n",
      "v1\n",
      "v0\n",
      "{'k2': 'v3', 'k1': 'v1'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(dic['k2'])            # 'v2'\n",
    "print(dic.get('k1'))        # 'v1'\n",
    "print(dic.get('k3', 'v0'))  # 'v0'\n",
    "\n",
    "dic['k2'] = 'v3'\n",
    "print(dic)                  # {'k1': 'v1', 'k2': 'v3'}\n",
    "\n",
    "print('k2' in dic)          # True\n",
    "print('v1' in dic)          # False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### III. Flow Control\n",
    "\n",
    "#### If"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Version 3.X\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "if sys.version_info.major < 3:\n",
    "    print(\"Version 2.X\")\n",
    "elif sys.version_info.major > 3:\n",
    "    print(\"Future\")\n",
    "else:\n",
    "    print(\"Version 3.X\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Loop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**for**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "for i in \"Hello\":\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**while**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "362880\n"
     ]
    }
   ],
   "source": [
    "prod = 1\n",
    "i = 1\n",
    "while i < 10:\n",
    "    prod = prod * i\n",
    "    i += 1\n",
    "print(prod)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found an even number  2\n",
      "Found an even number  4\n",
      "Found an even number  6\n",
      "n > 5!\n"
     ]
    }
   ],
   "source": [
    "## break & continue\n",
    "for n in range(2, 10):\n",
    "    if n % 2 == 0:\n",
    "        print(\"Found an even number \", n)\n",
    "        continue\n",
    "    if n > 5:\n",
    "        print(\"n > 5!\")\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found an even number 2\n",
      "Found a number 3\n",
      "Found an even number 4\n",
      "Found a number 5\n",
      "Found an even number 6\n",
      "Found a number 7\n",
      "Found an even number 8\n",
      "Found a number 9\n"
     ]
    }
   ],
   "source": [
    "## continue\n",
    "for num in range(2, 10):\n",
    "    if num % 2 == 0:\n",
    "        print(\"Found an even number\", num)\n",
    "        continue\n",
    "    print(\"Found a number\", num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comprehension"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**List**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 6, 8, 10, 12, 14, 16, 18]\n",
      "[(0, 0), (0, 1), (1, 0), (1, 1)]\n"
     ]
    }
   ],
   "source": [
    "s = [2 * x for x in range(10) if x ** 2 > 3]\n",
    "print(s)\n",
    "\n",
    "pairs = [(x, y) for x in range(2) for y in range(2)]\n",
    "print(pairs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Set**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{4, 6, 8, 10, 12, 14, 16, 18}\n",
      "{(0, 1), (1, 0), (0, 0), (1, 1)}\n"
     ]
    }
   ],
   "source": [
    "s = {2 * x for x in range(10) if x ** 2 > 3}\n",
    "print(s)\n",
    "\n",
    "pairs = set([(x, y) for x in range(2) for y in range(2)])\n",
    "print(pairs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Dict**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Python': 6, 'Javascript': 10, 'Golang': 6}\n",
      "{10: 'Javascript', 6: 'Golang'}\n"
     ]
    }
   ],
   "source": [
    "ls = {s: len(s) for s in [\"Python\", \"Javascript\", \"Golang\"]}\n",
    "print(ls)\n",
    "\n",
    "sl = {v: k for k, v in ls.items()}\n",
    "print(sl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Iterators & Generators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<str_iterator object at 0x10293f8d0>\n",
      "P\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "python = iter(\"Python\")\n",
    "print(python)\n",
    "for i in python:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object reverse at 0x1029539e8>\n",
      "n\n",
      "o\n",
      "h\n",
      "t\n",
      "y\n",
      "P\n"
     ]
    }
   ],
   "source": [
    "def reverse(data):\n",
    "    for index in range(len(data)-1, -1, -1):\n",
    "        yield data[index]\n",
    "nohtyp = reverse(\"Python\")\n",
    "print(nohtyp)\n",
    "for i in nohtyp:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### IV. Function\n",
    "#### Definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n",
      "return 'Hello, World!'\n"
     ]
    }
   ],
   "source": [
    "def f():\n",
    "    \"\"\"return 'Hello, World!'\"\"\"\n",
    "    return \"Hello, World!\"\n",
    "\n",
    "print(f())\n",
    "print(f.__doc__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n",
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "## default arguments\n",
    "def f(name = \"World\"):\n",
    "    \"\"\"return 'Hello, $name'\"\"\"\n",
    "    return \"Hello, {}!\".format(name)\n",
    "print(f())\n",
    "print(f(\"Python\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n",
      "Bye, C/C++!\n"
     ]
    }
   ],
   "source": [
    "## keyword arguments\n",
    "def f(v, l = \"Python\"):\n",
    "    \"\"\"return '$v, $l'\"\"\"\n",
    "    return \"{}, {}!\".format(v, l)\n",
    "print(f(\"Hello\"))\n",
    "print(f(\"Bye\", \"C/C++\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "Hello Python/C/C++\n"
     ]
    }
   ],
   "source": [
    "## arbitrary arguments\n",
    "def f(*args, con = \" & \"):\n",
    "    print(isinstance(args, tuple))\n",
    "    print(\"Hello\", con.join(args))\n",
    "\n",
    "f(\"Python\", \"C\", \"C++\", con = \"/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "args  ('Python', 'Javascript')\n",
      "kargs  {'ms': 'C++', 'fp': 'Haskell'}\n",
      "FP: Haskell & Scripts: Python/Javascript\n"
     ]
    }
   ],
   "source": [
    "def f(*args, **kargs):\n",
    "    print(\"args \", args)\n",
    "    print(\"kargs \", kargs)\n",
    "    print(\"FP: {} & Scripts: {}\".format(kargs.get(\"fp\"), \"/\".join(args)))\n",
    "    \n",
    "f(\"Python\", \"Javascript\", ms = \"C++\", fp = \"Haskell\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lambda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]\n",
      "[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n"
     ]
    }
   ],
   "source": [
    "pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n",
    "pairs.sort(key=lambda pair: pair[1])\n",
    "print(pairs)\n",
    "pairs.sort(key=lambda pair: pair[0])\n",
    "print(pairs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### @decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey log~\n",
      "This is fa!\n",
      "Bye log~\n",
      "**********\n",
      "Hey log~\n",
      "This is fb!\n",
      "Bye log~\n"
     ]
    }
   ],
   "source": [
    "def log(f):\n",
    "    def wrapper():\n",
    "        print(\"Hey log~\")\n",
    "        f()\n",
    "        print(\"Bye log~\")\n",
    "    return wrapper\n",
    "\n",
    "@log\n",
    "def fa():\n",
    "    print(\"This is fa!\")\n",
    "\n",
    "# Equal to...\n",
    "def fb():\n",
    "    print(\"This is fb!\")\n",
    "fb = log(fb)\n",
    "\n",
    "fa()\n",
    "print(\"*\"*10)\n",
    "fb()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### V. Class(OOP)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `class`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I can fly!\n",
      "This is an Animal\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    \"\"\"This is an Animal\"\"\"\n",
    "    def fly(_):\n",
    "        print(\"I can fly!\")\n",
    "a = Animal()\n",
    "a.fly()          # I can fly!\n",
    "print(a.__doc__) # This is an Animal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `__init__` & `self`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calling __init__() when instantiation!\n",
      "I can not fly!\n",
      "Calling __init__() when instantiation!\n",
      "I CAN fly!\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    \"\"\"This is an Animal\"\"\"\n",
    "    def __init__(self, can_fly = False):\n",
    "        print(\"Calling __init__() when instantiation!\")\n",
    "        self.can_fly = can_fly\n",
    "    def fly(self):\n",
    "        if self.can_fly:\n",
    "            print(\"I CAN fly!\")\n",
    "        else:\n",
    "            print(\"I can not fly!\")\n",
    "a = Animal()               # Calling __init__() when instantiation!\n",
    "a.fly()                    # I can not fly!\n",
    "\n",
    "b = Animal(can_fly = True) # Calling __init__() when instantiation!\n",
    "b.fly()                    # I CAN fly!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    pass\n",
    "class Human:\n",
    "    pass\n",
    "a = Animal()\n",
    "h = Human()\n",
    "print(isinstance(a, Animal))\n",
    "print(isinstance(h, Animal))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Inheritance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I can not fly!\n",
      "汪汪！\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    \"\"\"This is an Animal\"\"\"\n",
    "    def __init__(self, can_fly = False):\n",
    "        self.can_fly = can_fly\n",
    "    def fly(self):\n",
    "        if self.can_fly:\n",
    "            print(\"I CAN fly!\")\n",
    "        else:\n",
    "            print(\"I can not fly!\")\n",
    "class Dog(Animal):\n",
    "    \"\"\"This is a Dog\"\"\"\n",
    "    def bark(self):\n",
    "        print(\"汪汪！\")\n",
    "d = Dog()\n",
    "d.fly()\n",
    "d.bark()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Override"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm flying high!\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    \"\"\"This is an Animal\"\"\"\n",
    "    def __init__(self, can_fly = False):\n",
    "        self.can_fly = can_fly\n",
    "    def fly(self):\n",
    "        if self.can_fly:\n",
    "            print(\"I CAN fly!\")\n",
    "        else:\n",
    "            print(\"I can not fly!\")\n",
    "class Bird:\n",
    "    \"\"\"This is a Bird\"\"\"\n",
    "    def fly(self):\n",
    "        print(\"I'm flying high!\")\n",
    "bird = Bird()\n",
    "bird.fly()    # I'm flying high!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### VI. Module"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `import`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "posix\n",
      "VERSON: 3.5\n",
      "3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "print(os.name)\n",
    "\n",
    "from sys import version_info as PY_VERSION\n",
    "print(\"VERSON: {}.{}\".format(PY_VERSION.major, PY_VERSION.minor))\n",
    "\n",
    "from math import *\n",
    "print(pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Search Path\n",
    "\n",
    "1. current directory\n",
    "2. `echo $PYTHONPATH`\n",
    "3. `sys.path`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named 'MyModule.SubModule'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-40-c2d8d80de486>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Running MyModule.SubModuleOne.smo!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \"\"\"\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mMyModule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSubModule\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msmo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     12\u001b[0m \u001b[0msmo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;31m# Running MyModule.SubModuleOne.smo!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: No module named 'MyModule.SubModule'"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "MyModule/\n",
    "|--SubModuleOne/\n",
    "    |--__init__.py\n",
    "    |--smo.py\n",
    "\n",
    "# smo.py\n",
    "def run():\n",
    "    print(\"Running MyModule.SubModuleOne.smo!\")\n",
    "\"\"\"\n",
    "from MyModule.SubModule import smo\n",
    "smo.run()\n",
    "# Running MyModule.SubModuleOne.smo!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### VII. Pythonic\n",
    "\n",
    "### VIII. Standard Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
